package com.cb.controller.admin.order;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.command.QueryFilter;
import com.cb.core.command.QueryResult;
import com.cb.core.controller.BaseListController;
import com.cb.core.util.DateUtil;
import com.cb.domain.model.order.Policy;
import com.cb.domain.service.excelWork.POLICY_TYPE;
import com.cb.domain.service.file.ImageFileUpload;
import com.cb.domain.service.order.PolicyManager;
import com.cb.domain.service.system.ExlDataManager;

/**
 * 
 * @Author author
 * @Vesion 1.0
 * Create Time:
 * <p><b>Class Description:</b>[TODO]<p>
 */
public class PolicyController extends BaseListController {
	
	@Autowired
	private PolicyManager policyManager;
	@Autowired
	private ExlDataManager exlDataManager;

	
	/**对象列表页，提供最基本的分页显示
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		QueryFilter filter=new QueryFilter(request);
		String status=request.getParameter("Q_status_L_EQ");

		if(StringUtils.isEmpty(status)){
			filter.addFilter("Q_status_L_EQ", "1");
		}
		filter.addSorted("createTime", QueryFilter.SORT_DESC);
		QueryResult policys=policyManager.getAll(filter);
		
		return new ModelAndView("admin/order/policyList","policys",policys);
	/*	exlDataManager.postXmlContent("test");
		return new ModelAndView("redirect:/admin/order/showResult.do");*/
	}
	
	/**改变政策状态
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public void changPolicyStatus(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String message="fail";
		String status=request.getParameter("status");
		if(StringUtils.isNotBlank(status)){
			String[] policyIds=request.getParameterValues("policyId");
			if(policyIds!=null){
				for(String policyId:policyIds){
					Policy policy= policyManager.get(new Long(policyId));
					if(policy.getStatus()!=0&&policy.getStatus()!=Long.valueOf(status)){
						if(Long.valueOf(status)==3){//解挂操作
							policy.setStatus(1l);//1为有效
						}if(Long.valueOf(status)==2){//挂起操作
							policy.setStatus(Long.valueOf(status));//2为挂起
						}
						policyManager.save(policy);
					}
				}
				message="success";
			}
		}
		response.getWriter().print(message);
	}
	
	
	/**
	 * 删除全部政策
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void deleteAll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String message="";
		try {
			policyManager.deleteAll();
			message="success";
		} catch (Exception e) {
			message="fail";
		}
		response.getWriter().print(message);
	}
	
	/**
	 * 批量删除，通过页面传回对象的主键数组进行逐条删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		String[] policyIds=request.getParameterValues("policyId");
		if(policyIds!=null){
			for(String policyId:policyIds){
				policyManager.remove(new Long(policyId));
			}
			saveMessage(request, getText("policy.mutiDeleted"));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		
		
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 删除单个实体对象
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView delete(HttpServletRequest request,	HttpServletResponse response){
		String policyId=request.getParameter("policyId");
		if(StringUtils.isNotEmpty(policyId)){
			Policy policy=policyManager.get(new Long(policyId));
			policyManager.remove(policy);
			saveMessage(request,getText("policy.deleted",new Object[]{policy.getPolicyId()}));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	
	public ModelAndView goBatchUpload(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("admin/order/policyUpLoad");
	}
	
	/**
	 * 通过EXCEL文件批量导入政策
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView batchSavePolicy(HttpServletRequest request,HttpServletResponse response){
		String message="";
		String type=request.getParameter("uploadType");
		String path="";
		POLICY_TYPE doType=null;
		if(StringUtils.isNotBlank(type)){
		if(type.equals("1")){
			doType=POLICY_TYPE.COMMON;
		}else if(type.equals("2")){
			doType=POLICY_TYPE.LOWPRICE;
		}else if(type.equals("3")){
			doType=POLICY_TYPE.CUSTOMER;
		}else if(type.equals("4")){
			doType=POLICY_TYPE.APPLY;
		}else if(type.equals("5")){
			doType=POLICY_TYPE.PREPAY;
		}
		
		ImageFileUpload fileUpload=new ImageFileUpload(request,5);
		
		if (fileUpload.haveNewUploadFile()&& !fileUpload.isExit()) {
			fileUpload.saveFiles();
			 path = fileUpload.getFilePaths()[0];
		}
		 message="上传文件失败！";
		if(StringUtils.isNotBlank(path)){
			try{
				exlDataManager.doWorkByType(doType, path);
				List<Policy> list=exlDataManager.getPolicyList();
					if(null!=list&&list.size()>0){
						for(Policy policy:list){
							policy.setFlightCode(policy.getFlightCode().toUpperCase());
							policy.setCabin(policy.getCabin().replace(",", ""));
							policy.setPolicyType(Long.valueOf(type));
							policyManager.save(policy);
						}
						message="上传文件成功！";
					}
				}
			catch(Exception e){
					logger.error(e.getMessage(), e);
				}
			}
		}
		return new ModelAndView("admin/order/policyUpLoad").addObject("message", message);
	}
	
	/**
	 * 查看实体对象详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView viewDetail(HttpServletRequest request,	HttpServletResponse response){
		String policyId=request.getParameter("policyId");
		Policy policy=null;
		if(StringUtils.isNotEmpty(policyId)){
			policy=policyManager.get(new Long(policyId));
		}
		String viewName="admin/order/policyDetail";
		if(policy.getPolicyType()==1){
			viewName="admin/order/policyDetail";
		}else if(policy.getPolicyType()==2){
	    	viewName="admin/order/lowPricePolicyDetail";
		}else{
			viewName="admin/order/specialPolicyDetail";
		}
		return new ModelAndView(viewName,"policy",policy);
	}
}
